
/*
XMail OSAX -- Copyright (C) 2004, 2005, 2006, 2007 Jean-Baptiste LE STANG osax@lestang.org

	
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "auth-plain.h"


#include "smtp.h" 
#include "algorithm.h" 
#include "inetsocket.h" 
#include "smtp-string.h"

/**
 * auth_plain_init() will be called once when the plugin is loaded.
 */
void auth_plain_init ()    { /* do nothing */ }

/**
 * auth_plain_cleanup() will be called once when the plugin is 
 * unloaded.
 */
void auth_plain_cleanup () { /* do nothing */ }

/**
 * auth_plain_authenticate() is called by libsmtp after a positive
 * response to the EHLO command was received. All stuff
 * needed for authentication should be implemented here.
 * If auth_plain_authenticate() fails for some reason, -1 must be 
 * returned and an appropriate error message must be set with
 * the function err_set_error(). On success 0 is expected to 
 * be returned.
 *
 * @param srv   The smtp_srv_t object.
 *
 * @returns 0 on success, otherwise -1.
 */
int auth_plain_authenticate (smtp_srv_t * srv)
{
    char *cred, *enc, *rsp;
    int len, siz;

    len  = strlen (srv->username) + strlen (srv->password) + 3;
    cred = (char*)malloc (len);
    enc  = (char*)malloc (2*len+4);

    /* "\0USERNAME\0PASSWORD" */
    sprintf (cred, "%c%s%c%s", '\0', srv->username, '\0', srv->password);
    
    siz = alg_b64_encode (cred, len-1, enc);

    if (smtp_write (srv, "AUTH PLAIN ", 11) == -1){
        free(cred);free(enc);
        return (-1); /* error msg is already set */
    }

    if (smtp_write (srv, enc, siz) == -1){
        free(cred);free(enc);
        return (-1); /* error msg is already set */
    }
    
    if ((rsp = smtp_read_response_ (srv, 1)) == NULL){
        free(cred);free(enc);
        return (-1); /* error msg is already set */
    }

    free(rsp);free(cred);free(enc);
    return (0);
}

/**
 * auth_plain_read() will be called by libsmtp anytime it reads
 * from the underlying socket. If the SASL mechanism 
 * implements some sort of decryption, this is the place
 * for it.
 * If there is no need for decryption/modifying of the data
 * block, ssl_read() must be called using srv->sck, buf and 
 * bufsiz as parameter values.
 * If auth_plain_read() fails for some reason, -1 must be returned 
 * and an appropriate error message must be set with
 * the function err_set_error(). On success the number of
 * bytes that were actually stored in buf must be returned.
 *
 * @param srv     The smtp_srv_t object.
 * @param buf     The buffer that is to be filled.
 * @param bufsiz  The number of bytes that should be read at most.
 *
 * @returns The number of bytes that were stored in buf or -1 
 * if an error occured.
 */
int auth_plain_read (smtp_srv_t * srv, const char * buf, unsigned int bufsiz)
{
    return (ssl_read (&srv->sck, buf, bufsiz));
}

/**
 * auth_plain_write() will be called by libsmtp anytime it writes
 * to the underlying socket. If the SASL mechanism 
 * implements some sort of encryption, this is the place
 * for it.
 * If there is no need for encryption/modifying of the data
 * block, ssl_write() must be called using srv->sck, buf and 
 * bufsiz as parameter values.
 * If auth_plain_write() fails for some reason, -1 must be returned 
 * and an appropriate error message must be set with
 * the function err_set_error(). On success the number of
 * bytes that were actually written must be returned.
 *
 * @param srv     The smtp_srv_t object.
 * @param buf     The buffer that is to be filled.
 * @param bufsiz  The number of bytes that should be read at most.
 *
 * @returns The number of bytes actually written or -1 if an error
 * occured.
 */
int auth_plain_write (smtp_srv_t * srv, const char * buf, unsigned int bufsiz)
{
    return (ssl_write (&srv->sck, buf, bufsiz));
}

